package com.dmlll.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.dmlll.VO.SuperviseVO;
import com.dmlll.common.DataGridView;
import com.dmlll.common.ResultObj;
import com.dmlll.domain.Supervise;
import com.dmlll.service.SuperviseService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author dmLll
 * @since 2021-03-31
 */
@RestController
@RequestMapping("supervise")
public class SuperviseController {
    @Resource
    private SuperviseService superviseService;
    @RequestMapping("loadAllSupervise")
    public List<Supervise> loadAllSupervise(Integer type){
        if(type==2){
            return superviseService.list(new QueryWrapper<Supervise>().eq("type",1));
        }else {
            return superviseService.list(new QueryWrapper<Supervise>().eq("type",2));
        }
    }


    @RequestMapping("loadSupervise")
    public DataGridView loadSupervise(SuperviseVO superviseVO) {
        QueryWrapper<Supervise> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq(superviseVO.getType()!=null&&superviseVO.getType()>0,"type", superviseVO.getType());
        queryWrapper.like(StringUtils.isNotBlank(superviseVO.getSuperviseName()),"supervise_name",superviseVO.getSuperviseName());
        IPage<Supervise> iPage = superviseService.page(new Page<Supervise>(superviseVO.getPage(), superviseVO.getLimit()),queryWrapper);
        return new DataGridView(iPage.getTotal(), iPage.getRecords());
    }

    @RequestMapping("getSuperviseByAccount")
    public List<Supervise> getSuperviseByAccount(String account) {
        List<Supervise> list = superviseService.list(new QueryWrapper<Supervise>().eq("acount", account));
        return list;
    }

    @RequestMapping("add")
    public ResultObj add(Supervise supervise) {
        try {
            superviseService.save(supervise);
        } catch (Exception e) {
            return ResultObj.ADD_ERROR;
        }
        return ResultObj.ADD_SUCCESS;
    }

    @RequestMapping("update")
    public ResultObj update(Supervise supervise) {
        try {
            superviseService.updateById(supervise);
        } catch (Exception e) {
            return ResultObj.UPDATE_ERROR;
        }
        return ResultObj.UPDATE_SUCCESS;
    }
    @RequestMapping("delete")
    public ResultObj delete(Supervise supervise){
        try {
            superviseService.removeById(supervise.getId());
        } catch (Exception e) {
            return ResultObj.DELETE_ERROR;
        }
        return ResultObj.DELETE_SUCCESS;
    }
}

